<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 卡片 -->
      <el-card>
        <el-row>
          <el-col :span="6">
            <el-input v-model="queryInfo.username" placeholder="搜索"></el-input>
          </el-col>
          <el-col :span="2">
            <el-button size="small" type="primary" icon="el-icon-search" @click="getUserlist">搜索</el-button>
          </el-col>
          <el-col :span="2"><el-button size="small" type="primary" @click="resect">重置</el-button></el-col>
        </el-row>
        <!-- 新增用户 -->
        <el-row>
          <el-col class="addfather">
              <el-button size="small" icon="el-icon-edit" type="primary" class="add" @click="adddialogVisible = true">新增用户</el-button>
          </el-col>
        </el-row>
        <!-- 记录 -->
        <el-alert
    :title="`共${total}条记录`"
    type="info"
    show-icon
    class="sb"
    >
  </el-alert>
        <!-- 表格 -->
         <el-table
      :data="list"
      style="width: 100%">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="phone" label="联系电话"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="permission_group_title" label="权限组名称"></el-table-column>
      <el-table-column prop="role" label="角色"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="editUser(scope.row.id)" >编辑</el-button>
          <el-button size="small" type="danger" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
<el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.page"
      :page-sizes="[1,2,5,10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
      </el-card>
      <!-- 添加用户的对话框 -->
      <el-dialog
  title="创建用户"
  :visible.sync="adddialogVisible"
  width="55%">
  <!-- 表单 -->
  <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
  <el-form-item label="用户名" prop="username">
    <el-input v-model="addForm.username"></el-input>
  </el-form-item>
  <el-form-item label="邮箱" prop="email">
    <el-input v-model="addForm.email"></el-input>
  </el-form-item>
<el-form-item label="密码" prop="password">
    <el-input type="password" v-model="addForm.password"></el-input>
  </el-form-item>
  <el-form-item label="角色">
    <el-input v-model="addForm.role"></el-input>
  </el-form-item>
  <el-form-item label="权限名称">
     <el-select v-model="permission_group_id" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.permission_group_id"
      :label="item.label"
      :value="item.permission_group_id">
    </el-option>
  </el-select>
  </el-form-item>
  <el-form-item label="联系电话">
    <el-input v-model="addForm.phone"></el-input>
  </el-form-item>
  <el-form-item label="介绍">
    <el-input
  type="textarea"
  :rows="2"
  placeholder="请输入内容"
  v-model="textarea">
</el-input>
  </el-form-item>
</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="adddialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addUser">确 定</el-button>
  </span>
</el-dialog>
<!-- 编辑用户 -->
   <el-dialog
  title="编辑用户"
  :visible.sync="editdialogVisible"
  width="55%">
  <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
<el-form-item label="用户名" prop="username">
    <el-input v-model="editForm.username"></el-input>
  </el-form-item>
  <el-form-item label="邮箱" prop="email">
    <el-input v-model="editForm.email"></el-input>
  </el-form-item>
  <el-form-item label="角色">
    <el-input v-model="editForm.role"></el-input>
  </el-form-item>
  <el-form-item label="权限名称">
     <el-select v-model="permission_group_id" placeholder="请选择">
    <el-option
      v-for="item in options"
      :key="item.permission_group_id"
      :label="item.label"
      :value="item.permission_group_id">
    </el-option>
  </el-select>
  </el-form-item>
  <el-form-item label="联系电话">
    <el-input v-model="editForm.phone"></el-input>
  </el-form-item>
  <el-form-item label="介绍">
    <el-input
  type="textarea"
  :rows="2"
  placeholder="请输入内容"
  v-model="textarea">
</el-input>
  </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editSure">确 定</el-button>
  </span>
</el-dialog>
    </div>
  </div>
</template>
<script>
import { list, add, detail, update, remove } from '@/api/base/users'
export default {
  name: 'base-users',
  data () {
    return {
      input: '',
      queryInfo: {
        page: 1,
        pagesize: 5,
        keyword: '',
        username: ''
      },
      total: 0,
      id: '',
      list: [],
      adddialogVisible: false, // 添加用户的对话框
      editdialogVisible: false, // 编辑用户
      addForm: {
        avatar: '',
        email: '',
        introduction: '',
        password: '',
        permission_group_id: 3,
        phone: '',
        role: '',
        username: ''

      }, // 添加用户表单数据
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }, // 验证表单
      editFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
          // { min: 3, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ]
      },
      editForm: {}, // 编辑表单
      options: [{
        value: '选项1',
        label: '超级管理组'
      }, {
        value: '选项2',
        label: '编辑组'
      }],
      permission_group_id: '',
      textarea: ''// 介绍框
    }
  },
  created () {
    this.getUserlist()
  },
  methods: {
    async getUserlist () {
      try {
        const { data } = await list(this.queryInfo)
        this.list = data.list
        this.total = data.counts
        console.log(data)
      } catch (error) {
        this.$message.error('数据获取失败')
      }
    },
    // 添加用户
    async addUser () {
      try {
        const { data } = await add({
          avatar: this.addForm.avatar,
          email: this.addForm.email,
          introduction: this.addForm.introduction,
          password: this.addForm.password,
          permission_group_id: this.addForm.permission_group_id,
          phone: this.addForm.phone,
          role: this.addForm.role,
          username: this.addForm.username,
          sex: '男'
        })
        console.log(data)
        this.adddialogVisible = false
        this.getUserlist()
        this.addForm = ''
      } catch (error) {
        this.$message.error('添加失败')
      }
    },
    // 编辑用户
    async editUser (id) {
      this.editdialogVisible = true
      this.id = id
      try {
        const { data } = await detail({
          username: this.editForm.username,
          email: this.editForm.email,
          role: this.editForm.role,
          permission_group_id: this.editForm.permission_group_id,
          phone: this.editForm.phone,
          introduction: this.editForm.introduction,
          id: id
        }
        )
        this.editForm = data
        console.log(data)
      } catch (error) {
        this.$message.error('修改失败')
      }
      this.getUserlist()
    },
    async editSure () {
      try {
        const { data } = await update({
          username: this.editForm.username,
          email: this.editForm.email,
          role: this.editForm.role,
          permission_group_id: this.editForm.permission_group_id,
          phone: this.editForm.phone,
          introduction: this.editForm.introduction,
          id: this.id
        })
        console.log(data)
      } catch (error) {
        this.$message.error('修改失败')
      }
      this.editdialogVisible = false
      this.getUserlist()
    },
    async deleteUser (id) {
      const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (res !== 'confirm') {
        return this.$message.info('已取消操作')
      }
      await remove({ id })
      this.getUserlist()
      this.$message.success('删除成功')
    },
    // 删除
    // deleteUser (id) {
    //   this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(async () => {
    //     try {
    //       const { data } = await remove(id)
    //       this.getUserlist()
    //       console.log(data)
    //     } catch (error) {
    //       console.log('删除失败')
    //     }
    //     this.getUserlist()
    //   }).catch(() => {
    //     this.$message({
    //       type: 'info',
    //       message: '已取消删除'
    //     })
    //   })
    // },
    // 分页
    handleSizeChange (newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getUserlist()
    },
    handleCurrentChange (newPage) {
      console.log(newPage)
      this.queryInfo.page = newPage
      this.getUserlist()
    },
    // 重置
    resect () {
      this.queryInfo.username = ''
    }
  }
}
</script>

<style lang="scss" scoped>
.add {
  margin-top: 20px;
}
.el-alert {
  margin-top: 20px;
}
.sb {
  margin-bottom: 20px;
}
</style>
